#include "xbench/state.hpp"

using namespace std;

/**
 * xbench::State
 */

const string &xbench::State::name() const noexcept {
    return this->_name;
}

size_t xbench::State::calls() const noexcept {
    return this->_records.size();
}

int64_t xbench::State::duration_ns(size_t i) const{
    if(i < this->_records.size()){
        return this->_records[i].duration_ns();
    }
    return -1;
}

int64_t xbench::State::duration_ns() const{
    int64_t total = 0;
    for(auto& t: this->_records){
        auto ns = t.duration_ns();
        total += (ns != -1 ? ns : 0);
    }
    return total;
}

xbench::State::State(const xbench::State &state) {
    this->_name = state._name;
    this->_records = state._records;
}


/**
 * xbench::StateTable
 */
void xbench::StateTable::add_state(const xbench::State &state) {
    this->_states.push_back(&state);
}

const vector<const xbench::State *>& xbench::StateTable::get() const {
    return this->_states;
}

int64_t xbench::StateTable::total_nanoseconds() const {
    int64_t result = 0;
    for(const auto& state: this->_states){
        result += state->duration_ns();
    }
    return result;
}
